import 'package:abc/view/net_load_img.dart';
import 'package:abc/view/video_model.dart';
import 'package:abc/view/video_tag_utils.dart';
import 'package:flutter/material.dart';

// enum CommonVideoCellType {
//   twoLayout, //一排2个的布局
//   oneLayout, //一排1个的布局
//   threeLayout, //一排3个的布局
// }
enum CommonVideoCellType {
  AnimeLayout, //动漫
  MangaLayout, //漫画
  LongVideoLayout, //长视频
  ShortVideoLayout, //短视频
}

final threeLayoutConfig = CommonVideoCellConfig(
  imageHeight: 170,
  imageWidth: 105.0,
  tagHeight: 17.0,
  textHeight: 18.5,
  radius: 10,
  tagMargin: 4,
);

final twoLayoutConfig = CommonVideoCellConfig(
  imageHeight: 275.0,
  imageWidth: 168.0,
  tagHeight: 100.0,
  textHeight: 18.5,
  radius: 10,
  tagMargin: 40,
);

final animeLayoutConfig = CommonVideoCellConfig(
  imageHeight: 249.0,
  imageWidth: 168.0,
  tagHeight: 100.0,
  textHeight: 18.5,
  radius: 10,
  tagMargin: 40,
);

final oneLayoutConfig = CommonVideoCellConfig(
  imageHeight: 195 + 53,
  imageWidth: 346.0,
  tagHeight: 17.0,
  textHeight: 27.0,
  radius: 10,
  tagMargin: 12,
);

class CommonVideoCellConfig {
  final double imageHeight;
  final double imageWidth;
  final double tagHeight; //vip 金币 等级的背景高度
  final double radius;
  final double textHeight;
  final double tagMargin;

  CommonVideoCellConfig({
    required this.imageHeight,
    required this.imageWidth,
    required this.tagHeight,
    required this.radius,
    required this.textHeight,
    required this.tagMargin,
  });

  // 生成 getter 方法
  double get getImageHeight => imageHeight;
  double get getImageWidth => imageWidth;
  double get getTagHeight => tagHeight;
  double get getRadius => radius;
  double get getTextHeight => textHeight;
  double get getTagMargin => tagMargin;
}

class CommonVideoCell extends StatelessWidget {
  final VideoModel video;
  final VoidCallback onTap;
  final CommonVideoCellType layoutType;
  final CommonVideoCellConfig? layoutConfig;

  const CommonVideoCell({
    super.key,
    required this.video,
    required this.layoutType,
    required this.onTap,
    this.layoutConfig,
  });

  CommonVideoCellConfig getLayoutConfig() {
    if (layoutConfig != null) return layoutConfig!;
    if (layoutType == CommonVideoCellType.AnimeLayout ||
        layoutType == CommonVideoCellType.ShortVideoLayout) {
      return twoLayoutConfig;
    } else if (layoutType == CommonVideoCellType.MangaLayout) {
      return threeLayoutConfig;
    } else if (layoutType == CommonVideoCellType.LongVideoLayout) {
      return oneLayoutConfig;
    } else {
      return oneLayoutConfig;
    }
  }

  @override
  Widget build(BuildContext context) {
    final config = getLayoutConfig();
    print('getImageHeight=${layoutConfig!.getImageHeight}');
    if (layoutType == CommonVideoCellType.AnimeLayout) {
      return getAnimeLayoutWidget(config);
    } else if (layoutType == CommonVideoCellType.MangaLayout) {
      return getMangaLayoutWidget(config);
    } else if (layoutType == CommonVideoCellType.LongVideoLayout) {
      return getLongVideoLayoutWidget(config);
    } else if (layoutType == CommonVideoCellType.ShortVideoLayout) {
      return getShortVideoLayoutWidget(config);
    } else {
      return getLongVideoLayoutWidget(config);
    }
    return InkWell(
      onTap: () {
        // if (video.isValidAd) {
        //   Navigator.of(context).push(
        //     // MaterialPageRoute(
        //     //   builder: (context) => AdWebViewPage(
        //     //     url: video.adInfo?.adLink ?? '',
        //     //     title: video.adInfo?.adName ?? '',
        //     //   ),
        //     // ),
        //   );
        // } else {
        //   onTap();
        // }
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: config.imageWidth,
            height: config.imageHeight,
            clipBehavior: Clip.hardEdge,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(config.radius),
            ),
            child: Stack(
              children: [
                NetLoadImg(
                  video.isValidAd ? video.adInfo!.adImg : video.coverImage,
                  height: config.imageHeight,
                  width: config.imageWidth,
                  fit: BoxFit.cover,
                ),
                //vip或者金币
                if (!video.isValidAd)
                  Positioned(
                    left: config.tagMargin,
                    top: config.tagMargin,
                    child: _payTypeIcon(),
                  ),

                //时长
                if (!video.isValidAd)
                  Positioned(
                    bottom: config.tagMargin,
                    right: config.tagMargin,
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 5.0),
                      height: 15.0,
                      clipBehavior: Clip.hardEdge,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(7.5),
                        color: Colors.black.withOpacity(0.4),
                      ),
                      child: Text(
                        video.formatDuration,
                        style: TextStyle(
                          fontSize: 10.0,
                          fontWeight: FontWeight.w400,
                          color: Colors.blue,
                        ),
                      ),
                    ),
                  ),
              ],
            ),
          ),
          SizedBox(
            height: config.textHeight,
            child: Align(
              alignment: Alignment.centerLeft,
              child: Text(
                video.isValidAd ? video.adInfo!.adName : video.name,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 12.0,
                  fontWeight: FontWeight.w400,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget getLongVideoLayoutWidget(CommonVideoCellConfig config) {
    return Container(
      width: config.imageWidth,
      height: config.imageHeight,
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(config.radius),
      ),
      child: Column(
        children: [
          Stack(
            children: [
              NetLoadImg(
                video.isValidAd ? video.adInfo!.adImg : video.coverImage,
                height: config.imageHeight - 53,
                width: config.imageWidth,
                fit: BoxFit.fill,
              ),

              Positioned(right: 0, top: 0, child: _payTypeIcon()),
              Positioned(
                bottom: 5,
                left: 5,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 1),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(26),
                    color: Colors.white70,
                  ),
                  child: Center(
                    child: Text(
                      video.formatDuration,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 11,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.only(left: 10, top: 5),
            height: 53,
            width: config.imageWidth,

            decoration: BoxDecoration(
              color: Color(0x33959595),
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(config.radius), // 左下圆角
                bottomRight: Radius.circular(config.radius), // 右下圆角
                // 不设置 topLeft 和 topRight，默认无圆角
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  video.isValidAd ? video.adInfo!.adName : video.name,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                    height: 1.5,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Text(
                  '播放量${_formatWatchNum(video.watchNum)}',
                  style: TextStyle(
                    color: Color(0xFF999999),
                    fontSize: 11,
                    fontWeight: FontWeight.w400,
                    height: 1.5,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget getShortVideoLayoutWidget(CommonVideoCellConfig config) {
    //短视频
    return Container(
      width: config.imageWidth,
      height: config.imageHeight,
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(config.radius),
      ),
      child: Column(
        children: [
          Stack(
            children: [
              NetLoadImg(
                video.isValidAd ? video.adInfo!.adImg : video.coverImage,
                height: config.imageHeight - 53,
                width: config.imageWidth,
                fit: BoxFit.cover,
              ),
              Positioned(
                bottom: 6,
                right: 5,

                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 1),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(26),
                    color: Colors.white60,
                  ),
                  child: Center(
                    child: Text(
                      video.isAnime
                          ? video.updateStatusText
                          : video.formatDuration,
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 11,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.only(left: 10, top: 5),
            height: 53,
            width: config.imageWidth,

            decoration: BoxDecoration(
              color: Color(0x33959595),
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(config.radius), // 左下圆角
                bottomRight: Radius.circular(config.radius), // 右下圆角
                // 不设置 topLeft 和 topRight，默认无圆角
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  video.isValidAd ? video.adInfo!.adName : video.name,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                    height: 1.5,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Text(
                  '播放量${_formatWatchNum(video.watchNum)}',
                  style: TextStyle(
                    color: Color(0xFF999999),
                    fontSize: 11,
                    fontWeight: FontWeight.w400,
                    height: 1.5,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget getAnimeLayoutWidget(CommonVideoCellConfig config) {
    //动漫
    return Container(
      width: config.imageWidth,
      height: config.imageHeight,

      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              Container(
                width: config.imageWidth,
                height: config.imageHeight - 26,
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(config.radius),
                ),
                child: NetLoadImg(
                  video.isValidAd ? video.adInfo!.adImg : video.coverImage,
                  height: config.imageHeight,
                  width: config.imageWidth,
                  fit: BoxFit.cover,
                ),
              ),
              Positioned(right: 0, top: 0, child: _payTypeIcon()),
              Positioned(
                bottom: 5,
                left: 5,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 1),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(26),
                    color: Colors.white60,
                  ),
                  child: Center(
                    child: Text(
                      video.updateStatusText,
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 11,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 5),
          Text(
            video.isValidAd ? video.adInfo!.adName : video.name,
            style: TextStyle(
              color: Colors.white,
              fontSize: 15,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  Widget getMangaLayoutWidget(CommonVideoCellConfig config) {
    return Container(
      width: config.imageWidth,
      height: config.imageHeight,

      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              Container(
                width: config.imageWidth,
                height: config.imageHeight - 26,
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(config.radius),
                ),
                child: NetLoadImg(
                  video.isValidAd ? video.adInfo!.adImg : video.coverImage,
                  height: config.imageHeight,
                  width: config.imageWidth,
                  fit: BoxFit.cover,
                ),
              ),
              Positioned(
                right: 0,
                top: 0,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 7, vertical: 2),
                  decoration: BoxDecoration(
                    color: Color(0xFFFEA82B),
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(10),
                      topLeft: Radius.circular(10),
                      topRight: Radius.circular(8),
                    ),
                  ),
                  child: Center(
                    child: Text(
                      '本子',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 11,
                        fontWeight: FontWeight.w400,
                        height: 1,
                      ),
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: 5,
                left: 5,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 1),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(26),
                    color: Colors.white60,
                  ),
                  child: Center(
                    child: Text(
                      '共${video.commentNum}话',
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 11,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 5),
          Text(
            video.isValidAd ? video.adInfo!.adName : video.name,
            style: TextStyle(
              color: Colors.white,
              fontSize: 15,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  // 格式化播放量显示
  String _formatWatchNum(int watchNum) {
    if (watchNum >= 10000) {
      double wan = watchNum / 10000.0;
      // 保留一位小数
      return '${wan.toStringAsFixed(1)}万';
    }
    return watchNum.toString();
  }

  Widget _payTypeIcon() {
    return VideoTagUtils.buildPayTypeIcon(video);
  }
}
